package com.ayf.payment.game.api.service.platform.impl;

import cn.hutool.core.collection.CollUtil;
import com.ayf.payment.game.api.dto.platform.MerchantChannelDTO;
import com.ayf.payment.game.api.dto.platform.MerchantPaymentProductDTO;
import com.ayf.payment.game.api.dto.platform.PaymentChannelDTO;
import com.ayf.payment.game.api.dto.platform.PaymentChannelRateDTO;
import com.ayf.payment.game.api.entity.MerchantSettleRateCustom;
import com.ayf.payment.game.api.entity.platform.PaymentChannel;
import com.ayf.payment.game.api.entity.platform.PaymentChannelRate;
import com.ayf.payment.game.api.mapper.MerchantSettleRateCustomMapper;
import com.ayf.payment.game.api.mapper.platform.PaymentChannelMapper;
import com.ayf.payment.game.api.mapper.platform.PaymentProductMapper;
import com.ayf.payment.game.api.service.platform.PaymentChannelRateService;
import com.ayf.payment.game.api.service.platform.PaymentChannelService;
import com.ayf.payment.game.api.type.Status;
import com.ayf.payment.game.api.utils.KeyUtil;
import com.swwx.charm.commons.lang.utils.BeanConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class PaymentChannelServiceImpl implements PaymentChannelService {

    @Resource
    private PaymentChannelMapper paymentChannelMapper;

    @Autowired
    private PaymentChannelRateService paymentChannelRateService;

    @Resource
    private com.ayf.payment.game.api.config.SystemInitialization SystemInitialization;

    @Resource
    private MerchantSettleRateCustomMapper merchantSettleRateCustomMapper;

    @Resource
    private PaymentProductMapper paymentProductMapper;

    @Override
    public void insertPaymentChannel(PaymentChannelDTO dto) {
        PaymentChannel paymentChannel=new PaymentChannel();
        paymentChannel.setName(dto.getName());
        paymentChannel.setCode(dto.getCode());
        paymentChannel.setAccountNumber(dto.getAccountNumber());
        paymentChannel.setChannelFlag(1);

        Map<String, String> keyMap= KeyUtil.genKey();
        paymentChannel.setPlatformPublicKey(keyMap.get("PublicKey"));
        paymentChannel.setPlatformPrivateKey(keyMap.get("PrivateKey"));
        paymentChannel.setCreateTime(new Date());
        paymentChannel.setDescription(dto.getDescription());
        int paymentChannelId=paymentChannelMapper.insertPaymentChannel(paymentChannel);

        List<PaymentChannelRate> list=BeanConvertUtils.convert(dto.getChannelRateList(), PaymentChannelRate.class);
        if(list!=null && list.size()>0) {
            for (PaymentChannelRate pcr : list) {
                pcr.setPaymentChannelId(paymentChannelId);
                pcr.setStatus(1);
                PaymentChannelRate db=paymentChannelRateService.getPaymentChannelRate(pcr);
                if(db!=null){
                    paymentChannelRateService.updatePaymentChannelRateStatus(pcr);
                }else{
                    paymentChannelRateService.insertPaymentChannelRate(pcr);
                }
            }
        }
        SystemInitialization.InitializationData("增加支付通道");
    }



    @Override
    public List<PaymentChannelDTO> getAllPaymentChannel() {
        return paymentChannelMapper.selectPaymentChannelAll();
    }

    @Override
    public List<PaymentChannelDTO> selectPaymentChannelByType(int type) {
        return paymentChannelMapper.selectPaymentChannelByType(type);
    }

    @Override
    public PaymentChannelDTO getPaymentChannelDTO(int id) {
        PaymentChannel paymentChannel=paymentChannelMapper.selectByPrimaryKey(id);
        PaymentChannelDTO paymentChannelDTO = BeanConvertUtils.convert(paymentChannel, PaymentChannelDTO.class);
        List<PaymentChannelRate> list= paymentChannelRateService.getPaymentChannelRateByChannelId(paymentChannel.getId());
        List<PaymentChannelRateDTO> listDTO = BeanConvertUtils.convert(list, PaymentChannelRateDTO.class);
        paymentChannelDTO.setChannelRateList(listDTO);
        return paymentChannelDTO;
    }

    @Override
    public void updatePaymentChannel(PaymentChannelDTO paymentChannelDTO) {
        PaymentChannel paymentChannel=new PaymentChannel();
        paymentChannel.setId(paymentChannelDTO.getId());
        paymentChannel.setDescription(paymentChannelDTO.getDescription());
        paymentChannel.setAccountNumber(paymentChannelDTO.getAccountNumber());
        paymentChannel.setPaymentUrl(paymentChannelDTO.getPaymentUrl());
        paymentChannel.setChannelKey(paymentChannelDTO.getChannelKey());
        paymentChannel.setChannelPublicKey(paymentChannelDTO.getChannelPublicKey());
        paymentChannel.setId(paymentChannelDTO.getId());
        paymentChannelMapper.updateByPrimaryKeySelective(paymentChannel);
        List<PaymentChannelRate> list = BeanConvertUtils.convert(paymentChannelDTO.getChannelRateList(), PaymentChannelRate.class);
        if(list!=null && list.size()>0) {
            for (PaymentChannelRate pcr : list) {
                pcr.setPaymentChannelId(paymentChannelDTO.getId());
                PaymentChannelRate db=paymentChannelRateService.getPaymentChannelRate(pcr);
                if(db!=null){
                    paymentChannelRateService.updatePaymentChannelRateStatus(pcr);
                }else{
                    paymentChannelRateService.insertPaymentChannelRate(pcr);
                }
            }
        }
        SystemInitialization.InitializationData("更新支付通道");
    }

    @Override
    public void updatePaymentChannelUrl(PaymentChannelDTO paymentChannelDTO) {
//        PaymentChannel paymentChannel=new PaymentChannel();
//        paymentChannel.setId(paymentChannelDTO.getId());
//        paymentChannel.setPaymentUrl(paymentChannelDTO.getPaymentUrl());
//        paymentChannel.setReturnUrl(paymentChannelDTO.getReturnUrl());
        PaymentChannel channel = BeanConvertUtils.convert(paymentChannelDTO, PaymentChannel.class);
        paymentChannelMapper.updateByPrimaryKey(channel);
        SystemInitialization.InitializationData("更新支付通道");
    }

    @Override
    public void updatePaymentChannelRate(PaymentChannelDTO paymentChannelDTO) {
        List<PaymentChannelRate> list = BeanConvertUtils.convert(paymentChannelDTO.getChannelRateList(), PaymentChannelRate.class);
        if(list!=null && list.size()>0) {
            for (PaymentChannelRate pcr : list) {
                pcr.setPaymentChannelId(paymentChannelDTO.getId());
                paymentChannelRateService.updatePaymentChannelRate(pcr);
            }
        }
        SystemInitialization.InitializationData("更新支付通道费率");
    }

    @Override
    public void updatePaymentChannelStatus(PaymentChannelDTO paymentChannelDTO) {
        PaymentChannel paymentChannel=new PaymentChannel();
        paymentChannel.setId(paymentChannelDTO.getId());
        paymentChannel.setChannelFlag(paymentChannelDTO.getChannelFlag());
        paymentChannelMapper.updatePaymentChannelFlag(paymentChannel);

        paymentProductMapper.updateStatusByChannelId(paymentChannelDTO.getId(),paymentChannelDTO.getChannelFlag());

        List<Integer> productIds = paymentProductMapper.getProductIdsByChannelId(paymentChannelDTO.getId());
        if (!CollUtil.isEmpty(productIds)) {
            MerchantSettleRateCustom custom = new MerchantSettleRateCustom();
            custom.setProductIds(productIds);
            if (paymentChannelDTO.getChannelFlag() == 0) {
                custom.setStatus(Status.PROHIBIT);
            } else {
                custom.setStatus(Status.ENABLE);
            }
            merchantSettleRateCustomMapper.updateStatus(custom);
        }

        SystemInitialization.InitializationData("修改通道平台状态");
    }

    @Override
    public String updatePlatformKey(PaymentChannelDTO paymentChannelDTO) {
        PaymentChannel paymentChannel=new PaymentChannel();
        paymentChannel.setId(paymentChannelDTO.getId());
        Map<String, String> keyMap= KeyUtil.genKey();
        paymentChannel.setPlatformPublicKey(keyMap.get("PublicKey"));
        paymentChannel.setPlatformPrivateKey(keyMap.get("PrivateKey"));
        paymentChannelMapper.updateByPrimaryKeySelective(paymentChannel);
        SystemInitialization.InitializationData("修改通道平台KEY");
        return paymentChannel.getPlatformPublicKey();

    }



    @Override
    public void updateMerchantChannel(MerchantChannelDTO merchantChannelDTO) {
        paymentChannelMapper.delMerchantChannel(merchantChannelDTO.getMerchantId());
        paymentChannelMapper.insertMerchantChannel(merchantChannelDTO);
    }

    @Override
    public Integer[] getPaymentChannelByMerchantId(MerchantChannelDTO merchantChannelDTO) {
        return paymentChannelMapper.getPaymentChannelByMerchantId(merchantChannelDTO.getMerchantId());
    }

    @Override
    public Integer[] getPaymentChannelByDefault() {
        return paymentChannelMapper.getPaymentChannelByDefault();
    }

    @Override
    public List<PaymentChannelDTO> selectPaymentChanne() {
        return paymentChannelMapper.selectPaymentChannel();
    }
    @Override
    public PaymentChannelDTO getPaymentChanneByCId(Integer cId){
           return paymentChannelMapper.getPaymentChannelByCid(cId);
    }

    @Override
    public List<MerchantPaymentProductDTO> selectMerchantPaymentProduct(Integer channelId) {
        return paymentChannelMapper.selectMerchantPaymentProduct(channelId);
    }

    public MerchantPaymentProductDTO getMerchantPaymentProductByPId(Integer pId){
        return paymentChannelMapper.getMerchantPaymentProduct(pId);
    }

    @Override
    public PaymentChannelDTO getPaymentChannelByCode(String code) {
        return paymentChannelMapper.getPaymentChannelByCode(code);
    }

    @Override
    public PaymentChannelDTO getPaymentChannelById(Integer id) {
        return BeanConvertUtils.convert(paymentChannelMapper.selectByPrimaryKey(id),PaymentChannelDTO.class);
    }
}

